package com.imau.common.entiry;

import com.imau.bean.Permission;
import com.imau.bean.Role;
import com.imau.bean.User;
import com.imau.common.Const.PermissionConst;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 前端个人信息封装实体类结果
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProfileResult implements Serializable {

    private String mobile;
    private String username;

    private Map<String, Object> roles = new HashMap<>();


    public ProfileResult(User user) {
        this.mobile = user.getMobile();
        this.username = user.getUsername();
        Set<Role> roles1 = user.getRoles();
        Set<String> menus = new HashSet<>();
        Set<String> points = new HashSet<>();
        Set<String> apis = new HashSet<>();

        for (Role role : roles1) {

            Set<Permission> permissions = role.getPermissions();
            for (Permission perm : permissions){

                if(perm.getType()== PermissionConst.PERMISSION_MENU)
                {
                    menus.add(perm.getCode());
                }else if(perm.getType()== PermissionConst.PERMISSION_POIN){
                    points.add(perm.getCode());
                }else {
                    apis.add(perm.getCode());
                }

            }
        }

        roles.put("menus",menus);
        roles.put("points",points);
        roles.put("apis",apis);
    }

}
